package com.xinsoft.controller.basic;

import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.GetFileTypeByHeadUtil;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.SysFileParam;
import com.xinsoft.entity.po.SysFileHis;
import com.xinsoft.service.SysFileHisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysFileController
 * @Description 文件管理控制层
 * @Author lijie
 * @Date 2023-5-19 12:35:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/file")
@Api(value = "SysFileController", tags = {"文件管理控制层"})
public class SysFileController {
    @Autowired
    private SysFileHisService fileHisService;

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 文件管理-分页
     * @Date 2023-5-19 12:35:53
     * @params param
     **/
    @PostMapping("/pageList")
    public Response<?> pageList(@Valid @RequestBody SysFileParam param) {
        return Response.succeed(fileHisService.findPageListByParam(param));
    }

    @PostMapping(value = "/upload")
    @ApiOperation("单文件上传")
    @SysLog(title = "文件上传", businessType = BusinessType.OTHER)
    public Response<?> upload(@ApiParam(value = "文件信息", required = true) @RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Response.fail("未选中文件");
        }
        //判断是否为可执行文件
        String fileHeader = GetFileTypeByHeadUtil.getFileTypeByInputStream(file.getInputStream());
        if (StringUtils.isNotBlank(fileHeader)) {
            return Response.fail("文件校验失败");
        }
        Map<String, Object> result = new HashMap<>();
        try {
            String path = fileHisService.uploadFilesByIO(file);
            result.put("fileName", file.getOriginalFilename());
            result.put("filePath", path);
            return Response.succeed(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail("文件上传失败");
        }
    }

    @PostMapping(value = "/upload/business")
    @ApiOperation("业务文件上传")
    @SysLog(title = "业务文件上传", businessType = BusinessType.OTHER)
    public Response<?> uploadBusiness(@ApiParam(value = "文件信息", required = true) @RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return Response.fail("未选中文件");
        }
        //判断是否为可执行文件
        String fileHeader = GetFileTypeByHeadUtil.getFileTypeByInputStream(file.getInputStream());
        if (StringUtils.isNotBlank(fileHeader)) {
            return Response.fail("文件校验失败");
        }
        try {
            SysFileHis sysFileHis = fileHisService.uploadCusFile(file);
            return Response.succeed(sysFileHis);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail("文件上传失败");
        }
    }

    @PostMapping(value = "/uploads")
    @ApiOperation("多文件上传")
    @SysLog(title = "多文件上传", businessType = BusinessType.OTHER)
    public Response<?> uploads(@ApiParam(value = "文件信息", required = true) @RequestParam("files") MultipartFile[] files) throws IOException {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> pathList = new ArrayList<>();
        for (MultipartFile file : files) {
            if (file.isEmpty()) {
                return Response.fail("未选中文件");
            }
            //判断是否为可执行文件
            String fileHeader = GetFileTypeByHeadUtil.getFileTypeByInputStream(file.getInputStream());
            if (StringUtils.isNotBlank(fileHeader)) {
                return Response.fail("文件校验失败");
            }
            try {
                Map<String, Object> fileMap = new HashMap<>();
                String path = fileHisService.uploadFilesByIO(file);
                fileMap.put("fileName", file.getOriginalFilename());
                fileMap.put("filePath", path);
                pathList.add(fileMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        result.put("result", pathList);
        return Response.succeed(result);
    }

}
